(function() {
    // getting a reference to the main namespace if it exists - empty object otherwise
    var Prestatheme = window.Prestatheme = window.Prestatheme || {};
	
    Prestatheme.applyStyle = function(select, attr, val) 
    {
        try
        {
            Prestatheme.runDebug ("[APPLY-STYLE][applyStyle] Refresh all the updated elements");
			
			if (attr != '' && val !='')
			{
				$('iframe#previewer').contents().find(select).css(attr, val);
			}
        }
        catch (e)
        {
            Prestatheme.manageError ('APLS', e);
        }
    };
	
    Prestatheme.applyDesign = function(elemName, targetElem, classChoosen) 
    {
        try
        {
            Prestatheme.runDebug ("[APPLY-STYLE][applyDesign] Apply the design choosen by the user");
			
			if (elemName != '' && targetElem !='' && classChoosen != '')
			{
				// Remove incompatible classes
				$('input[name="'+ elemName +'"]').each(function () {
					$('iframe#previewer').contents().find(targetElem).removeClass($(this).val());
				});
				
				// Add the selected class
				$('iframe#previewer').contents().find(targetElem).addClass(classChoosen);
			}
        }
        catch (e)
        {
            Prestatheme.manageError ('APLD', e);
        }
    };
	
    Prestatheme.refresh = function() 
    {
		var item, stylesToString = '';
        try
        {
            Prestatheme.runDebug ("[APPLY-STYLE][refresh] Refresh all the updated elements");
			
			// Refresh styles
			if (Prestatheme.styles == undefined)
				Prestatheme.styles = new Array();
			
			for (var i=0; i<Prestatheme.styles.length; i++)
			{
				item = Prestatheme.styles[i];
				if (item.attribute != '' && item.value !='')
				{
					$('iframe#previewer').contents().find(item.selector).css(item.attribute, item.value);
				}
			}
			
			// Refresh design
			if (Prestatheme.designCfg == undefined)
				Prestatheme.designCfg = new Array();
			
			for (var i=0; i<Prestatheme.designCfg.length; i++)
			{
				item = Prestatheme.designCfg[i];
				if (item.elemName != '' && item.targetElem != '' && item.classChoosen != '')
				{
					// Remove incompatible classes
					$('input[name="'+ item.elemName +'"]').each(function () {
						$('iframe#previewer').contents().find(item.targetElem).removeClass($(this).val());
					});
					
					// Add the selected class
					$('iframe#previewer').contents().find(item.targetElem).addClass(item.classChoosen);
				}
			}
        }
        catch (e)
        {
            Prestatheme.manageError ('RFS', e);
			return stylesToString;
        }
    };
})();